import java.awt.Color;
import java.util.Random;

import javax.swing.UIManager;


public class Bot extends Player
{
	
	Bot(Table T,Deck deck, String x, String y, int coins) {
		super(T,deck, x, y, coins);
		
		C1.setForeground(UIManager.getColor("Button.background"));
		C2.setForeground(UIManager.getColor("Button.background"));
		C3.setForeground(UIManager.getColor("Button.background"));
		C4.setForeground(UIManager.getColor("Button.background"));
		C5.setForeground(UIManager.getColor("Button.background"));
		
		ExchangeButton.setEnabled(false);
		ReadyButton.setEnabled(false);
		CheckButton.setEnabled(false);
		RaiseButton.setEnabled(false);
		CallButton.setEnabled(false);
		FoldButton.setEnabled(false);
		AllInButton.setEnabled(false);
		ExitButton.setEnabled(false);
	}
	
	
	
	@Override
	void exchangeTime()
	{
		int k=1,x;
		Random roll=new Random();//Generator of random numbers
		if((roll.nextInt(100)+1)<=k) // k is the % chance of going in blind
		{
			// go ready
			
			readyCheck();
		}
		else
		{
			int[] result=C.CardCheck(this);
			//System.out.println("result"+result);
			
			switch(result[0])
			{
			case 8://Straight Flush
				//go ready
															//raise 100% all in if not enough
				readyCheck();
				break;
			case 7://Four of a Kind
				if (result[2]==14)
				{
					//go ready
															//raise 100% all in if not enough
					readyCheck();
				}
				else
				{
					if((result[2]>9&&(roll.nextInt(100)+1)<25)||result[2]<=9) //got a 25% chance to exchange the last card even if it's higher than 9
					{
							//exchange 1 card
						{
							for(int i=0;i<5;i++)
							{
								if(result[2]==hand[i].Value())
								{
									cardFlags[i]=1;
									
									break;
								}
							}
							exchangeCards();
							
						}
							//go ready
						
						readyCheck();
					}
					else
					{
						//go ready
						
						readyCheck();
					}
				}
				break;
			case 6://Full House
				//go ready
																	//raise 100%, all in if not enough
				readyCheck();
				break;
			case 5://Flush
				//go ready
																	//raise 80%, call 20%, all in if not enough
				readyCheck();
				break;
			case 4://Straight
				//go ready
																	//raise 80%, call 20%, all in if not enough
				readyCheck();
				break;
			case 3://Three of a Kind
				if((roll.nextInt(100)+1)<10)
				{//exchange 2 cards
					for(int i=0;i<5;i++)
					{
						if(result[1]!=hand[i].Value())
						{
							cardFlags[i]=1;
						}
					}
					exchangeCards();
				}
				//go ready
																	//raise 100% all in if not enough
				readyCheck();
				break;
			case 2://Two Pair
				if (result[3]==14)
				{
					//go ready
					
					readyCheck();
				}
				else
				{
					if((result[3]>9&&(roll.nextInt(100)+1)<25)||result[3]<=9) //got a 25% chance to exchange the last card even if it's higher than 9
					{
							//exchange 1 card
						for(int i=0;i<5;i++)
						{
							if(result[3]==hand[i].Value())
							{
								cardFlags[i]=1;
								break;
							}
						}
						exchangeCards();
							//go ready
						
						readyCheck();
					}
					else
					{
						//go ready
						
						readyCheck();
					}
				}
				break;
			case 1://One Pair
				x=roll.nextInt(100)+1;
				if (x>50)
				{
					//exchange 3 cards
					for(int i=0;i<5;i++)
					{
						if(result[1]!=hand[i].Value())
						{
							cardFlags[i]=1;
						}
					}
					exchangeCards();
					//go ready
					
					readyCheck();
				}
				else
				{
					if(x>30)
					{
						//exchange 2 cards
						int temp0=0, i;
						for (i=0;i<5;i++)
						{
							if((hand[i].Value()>temp0)&&(hand[i].Value()!=result[1]))
							{
								temp0=hand[i].Value();
							}
						}
						for(i=0;i<5;i++)
						{
							if((result[1]!=hand[i].Value())&&temp0!=hand[i].Value())
							{
								cardFlags[i]=1;
							}
						}
						exchangeCards();
						//go ready
						
						readyCheck();
					}
					else
					{
						if (x>10)
						{
							//exchange 1 card
							int temp0=0,temp1=0, i;
							for (i=0;i<5;i++)
							{
								if((hand[i].Value()>temp0)&&(hand[i].Value()!=result[1]))
								{
									temp1=temp0;
									temp0=hand[i].Value();
								}
							}
							for(i=0;i<5;i++)
							{
								if((result[1]!=hand[i].Value())&&(temp0!=hand[i].Value())&&(temp1!=hand[i].Value()))
									cardFlags[i]=1;
							}
							exchangeCards();
							//go ready
						
							readyCheck();
						}
						else
						{
							//go ready
							
							readyCheck();
						}
							
							
					}
							
				}
				break;
			case 0://High Card 
				//look for flush then straight
				{
					Card check;
					int c=0,s=0,tempC=0;
					char tempColour=hand[0].colour;
					for (int j=0;j<4;j++)
					{
						check=hand[j];
						for (int i=j;i<5;i++)
						{
							if(check.colour==hand[i].colour)
							{
								c++;
							}
						}
						if(tempC<c)
						{
							tempC=c;
							tempColour=hand[j].colour;
						}
					}
					if(c>2)
					{
						//exchange all that don't match tempColour
						for(int i=0;i<5;i++)
						{
							if(tempColour!=hand[i].colour)
							{
								cardFlags[i]=1;
							}
						}
						exchangeCards();
						//go ready
						
						readyCheck();
						break;
					}
					else //then
					{
						//Going for Straight is too risky for us, Botz! We don't want this function!
					}
					
				}
				//nothing found
				{
					x=roll.nextInt(100)+1;
					if (x>40)
					{ //exchange 4 cards
						for(int i=0;i<5;i++)
						{
							if((result[1]!=hand[i].Value()))
							{
								cardFlags[i]=1;
							}
						}
						exchangeCards();
						//go ready
						
						readyCheck();
					}
					else
					{
						if(x>20)
						{
							//exchange 3 cards
							for(int i=0;i<5;i++)
							{
								if((result[1]!=hand[i].Value())&&(result[2]!=hand[i].Value()))
								{
									cardFlags[i]=1;
								}
							}
							exchangeCards();
							//go ready
							
							readyCheck();
						}
						else
						{
							if (x>5)
							{
								//exchange 1 card
								for(int i=0;i<5;i++)
								{
									if((result[5]==hand[i].Value()))
									{
										cardFlags[i]=1;
										break;
									}
								}
								exchangeCards();
								//go ready
								
								readyCheck();
							}
							else
							{
									//go ready
								
								readyCheck();
							}
							
							
						}
							
					}
				}
				break;	
			}//end switch
			
		}
		
	}//end startTurn
	
	
	@Override
	void bidTime()
	{
		int x[]=availability();
		Random roll=new Random();//Generator of random numbers
		{
			int[] result=C.CardCheck(this);
			//System.out.println("result"+result);
			if (coins<=T.ante)
			{
				allIn();
			}
			else
			{
			switch(result[0])
			{
			case 8://Straight Flush
				//go ready
				if (T.VaBanque==1)
				{
					allIn();
				}
				else if((roll.nextInt(100)+1)>=95 & x[1]!=1)//raise 100% all in if not enough
				{
					call();
				}
				else if (x[2]!=1)
					raise();
				else
					allIn();
				break;
			case 7://Four of a Kind
					//go ready
															//raise 100% all in if not enough
					if (T.VaBanque==1)
					{
						allIn();
					}
					if((roll.nextInt(100)+1)>=95 & x[1]!=1)//raise 100% all in if not enough
					{
						call();
					}
					else if (x[2]!=1)
						raise();
					else
						allIn();
				break;
			case 6://Full House
				//go ready
				if (T.VaBanque==1)
				{
					allIn();
				}													//raise 100%, all in if not enough
				if((roll.nextInt(100)+1)>=90)//raise 100% all in if not enough
				{
					call();
				}
				else if (x[2]!=1)
					raise();
				else
					allIn();
				break;
			case 5://Flush
				//go ready
				if (T.VaBanque==1)
				{
					allIn();
				}													//raise 80%, call 20%, all in if not enough
				if((roll.nextInt(100)+1)>=90)//raise 100% all in if not enough
				{
					call();
				}
				else if (x[2]!=1)
					raise();
				else
					allIn();
				break;
			case 4://Straight
				//go ready
																	//raise 80%, call 20%, all in if not enough
				if (T.VaBanque==1)
				{
					allIn();
				}	
				if((roll.nextInt(100)+1)>=80 & x[1]!=1)//raise 100% all in if not enough
				{
					call();
				}
				else if (x[2]!=1)
					raise();
				else
					allIn();
				break;
			case 3://Three of a Kind
				if (T.VaBanque==1)
				{
					if(roll.nextInt(100)+1<=5)
						allIn();
					else
						fold();
				}	
				else if ((roll.nextInt(100)+1<=25 | roll.nextInt(100)+1<bluff) & x[2]!=1)
				{
					raise();
					bluff=100-T.bidsDone;
				}
				else if (roll.nextInt(100)+1<=(cashSpent*0.8/(coins+cashSpent))*100 & x[4]!=cashSpent)
				{
					fold();
				}
				else if((roll.nextInt(100)+1)<=(cashSpent/(coins+cashSpent))*100 & (x[2]!=1))//raise 100% all in if not enough
				{
					raise();
				}
				else if (x[1]!=1)
					call();
				else 
					allIn();
				break;
			case 2://Two Pair
				if (T.VaBanque==1)
				{
					if(roll.nextInt(100)+1<=3)
						allIn();
					else
						fold();
				}
				else if ((roll.nextInt(100)+1<=15 | roll.nextInt(100)+1<bluff) & x[2]!=1)
				{
					raise();
					bluff=100-T.bidsDone;
				}
				else if (((roll.nextInt(100)+1)<=(cashSpent/(coins+cashSpent))*100) & x[4]!=cashSpent)
				{
					//System.out.println(cashSpent/coins+cashSpent*100+" "+y);
					fold();
				}
				else if((roll.nextInt(100)+1)<=(cashSpent/(coins+cashSpent))*100 & x[2]!=1)//raise 100% all in if not enough
				{
					raise();
				}
				else if (x[1]!=1)
					call();
				else
					allIn();
				break;
			case 1://One Pair
				if (T.VaBanque==1)
				{
					if(roll.nextInt(100)+1<=1)
						allIn();
					else
						fold();
				}
				else if ((roll.nextInt(100)+1<=5 | roll.nextInt(100)+1<bluff) & x[2]!=1)
				{
					raise();
					bluff=100-T.bidsDone;
				}
				else if (roll.nextInt(100)+1<=(cashSpent*1.1/(coins+cashSpent))*100 & x[4]!=cashSpent)
				{
					//System.out.println(cashSpent*1.1/(coins+cashSpent)*100+" "+y);
					fold();
				}
				else if((roll.nextInt(100)+1)>=(cashSpent/(coins+cashSpent))*100 & x[2]!=1)//raise 100% all in if not enough
				{
					raise();
				}
				else if (x[1]!=1)
					call();
				else
					allIn();
				break;
			case 0://High Card 
				if (T.VaBanque==1)
				{
						fold();
				}
				else if ((roll.nextInt(100)+1<=2 | roll.nextInt(100)+1<bluff) & x[2]!=1)
				{
					raise();
					bluff=100-T.bidsDone;
					//System.out.println(bluff+"bluff"+y);
				}
				else if (roll.nextInt(100)+1<=(cashSpent*1.5/(coins+cashSpent))*100 & x[4]!=cashSpent)
				{
					//System.out.println(cashSpent*3/(coins+cashSpent)*100+" "+y);
					fold();
				}
				else if((roll.nextInt(100)+1)>=(cashSpent/(coins+cashSpent))*100 & x[2]!=1)//raise 100% all in if not enough
				{
					raise();
				}
				else if (x[1]!=1)
					call();
				else
					allIn();
				break;	
			}//end switch
			
		}
		}		
	}//end startTurn
	

}
